package com.his.hospital.controller;

import com.his.config.annotation.CurrentUser;
import com.his.hospital.service.HospitalService;
import com.his.hospital.vo.HospitalVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/6 11:21
 * @Version 1.0
 */
@RestController
@RequestMapping(value="/api/hospital")
public class HospitalController extends ResultBaseController {

    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    @Resource
    private HospitalService hospitalService;

    @PostMapping(value = "/save.do")
    public ResultUtil save(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated HospitalVo hospitalVo){
        try{
            hospitalService.save(dictEmployee,hospitalVo);
            return this.success("保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }

    @PostMapping(value = "/update.do")
    public ResultUtil update(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated HospitalVo hospitalVo){
        try{
            hospitalService.update(hospitalVo);
            return this.success("保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }

    @GetMapping(value = "/get_by_id.do")
    public ResultUtil getByOrgId(@CurrentUser DictEmployee dictEmployee, String orgId){
        try{
            return this.success("",hospitalService.selectById(orgId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }

    /**
     * 获取全部医院
     * @return
     */
    @GetMapping(value="/get_all.do")
    public ResultUtil getHosp(@CurrentUser DictEmployee dictEmployee){
        try{
            return this.success("",hospitalService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
